package com.ITArmy.controller.otherLogin;

import com.ITArmy.beans.User;
import com.ITArmy.dao.UserMapper;
import com.github.pagehelper.Page;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

@Controller

@RequestMapping("/qq")
public class QQ {
    @Autowired
    UserMapper userMapper;

    @RequestMapping(value = "/login")
    public void qqlogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
//设置相应编码
//System.out.println("111");测试使用的
        response.setContentType("text/html;charset=utf-8");

        try {
//跳转到qq第三方登录界面
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    @RequestMapping(value = "/CallBack", method = RequestMethod.GET)
    public String qqSuccess(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException, QQConnectException {
        System.out.println("我运行了...");
        Page page = new Page();
        response.setContentType("text/html;charset=utf-8");
        String accessToken = null, openID = null;
        String qqname = null;
        String gender = null;
        String url50 = null;
        long tokenExpireIn = 0L;
        try {
            AccessToken accessTokenObj = (new Oauth().getAccessTokenByRequest(request));
            if (accessTokenObj.getAccessToken().equals("")) {
                System.out.println("没有获取到响应参数");
            } else {
                accessToken = accessTokenObj.getAccessToken();
                tokenExpireIn = accessTokenObj.getExpireIn();
                request.getSession().setAttribute("demo_access_token", accessToken);
                request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));
                // 利用获取到的accessToken 去获取当前用的openid -------- start
                OpenID openIDObj = new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();
                request.getSession().setAttribute("demo_openid", openID);
                // 利用获取到的accessToken 去获取当前用户的openid --------- end

                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                System.out.println(userInfoBean);
                if (userInfoBean.getRet() == 0) {
                    //QQ空间姓名
                    qqname = userInfoBean.getNickname();
                    System.out.println("名字为："+qqname);
                    //QQ空间性别
                    gender = userInfoBean.getGender();
                    //QQ空间头像
                    url50 = userInfoBean.getAvatar().getAvatarURL50();
                    //查询用户表中是否有这个用户
                    User user = userMapper.selectUserByqqOpenId(openID);
                    if (user == null) {
                        //进行新注册，信息储存到session中，请求转发
                        User user1 = new User();
                        user1.setQqId(openID);
                        user1.setuHead(url50);
                        user1.setuTime(new Date());
                        user1.setuName(qqname);
                        user1.setuManage("0");
                        user1.setCoId(1);
                        userMapper.insert(user1);
                        //将其从数据库中查询出来
                        HttpSession session = request.getSession();
                        session.setAttribute("user", userMapper.selectUserByqqOpenId(openID));
                        return "redirect:/user/index";
                    } else {
                        //获取用户的信息储存到session中，请求转发
                        HttpSession session = request.getSession();
                        session.setAttribute("user", user);
                        return "redirect:/user/index";
                    }
                } else {
                    System.out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
                }
            }
        } catch (QQConnectException e) {
            e.printStackTrace();
            System.out.println("QQ登录失败，原因：" + e.getMessage());
            return "login";
        }
        return "sign";
    }
}

